import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AppLayout from '@/views/layout/AppLayout.vue';

import { useUserStore } from "@/stores/modules/user";
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            name: 'home',
            component: AppLayout,
            children: [
                {
                    path: '/',
                    name: 'dashboard',
                    component: () => import('@/views/Dashboard.vue')
                },
                {
                    path: '/menu/create',
                    name: 'create',
                    component: () => import('@/views/pages/menu/myExam/create.vue')
                },
                {
                    path: '/menu/paper',
                    name: 'paper',
                    component: () => import('@/views/pages/menu/paper/paper.vue')
                },
                {
                    path: '/menu/test',
                    name: 'test',
                    component: () => import('@/views/pages/menu/myExam/test.vue')
                },
                {
                    path: '/menu/class',
                    name: 'class',
                    component: () => import('@/views/pages/menu/class/Class.vue')
                }, {
                    path: '/menu/student',
                    name: 'student',
                    component: () => import('@/views/pages/menu/student/Student.vue')
                },
            ]
        },

        {
            path: '/about',
            name: 'about',
            // route level code-splitting
            // this generates a separate chunk (About.[hash].js) for this route
            // which is lazy-loaded when the route is visited.
            component: () => import('../views/AboutView.vue')
        },
        //   {
        //     path: '/landing',
        //     name: 'landing',
        //     component: () => import('@/views/pages/Landing.vue')
        // },
        // {
        //     path: '/pages/notfound',
        //     name: 'notfound',
        //     component: () => import('@/views/pages/NotFound.vue')
        // },

        {
            path: '/auth/login',
            name: 'login',
            component: () => import('@/views/pages/auth/Login.vue')
        },
        {
            path: '/auth/register',
            name: 'register',
            component: () => import('@/views/pages/auth/Register.vue')
        },
        // {
        //     path: '/auth/access',
        //     name: 'accessDenied',
        //     component: () => import('@/views/pages/auth/Access.vue')
        // },
        // {
        //     path: '/auth/error',
        //     name: 'error',
        //     component: () => import('@/views/pages/auth/Error.vue')
        // }
        {
            path: '/:pathMath(.*)*',
            name: 'NotFound',
            component: () => import('@/views/pages/auth/Access.vue'),
        },
    ]
})
router.beforeEach((to, from, next) => {
    //to到哪儿 from从哪儿离开 next跳转 为空就是放行
    if (to.path === '/auth/login' || to.path === '/auth/register') {
        //如果跳转为登录，就放行
        next();
    } else {
        //取出localStorage判断
        // let token = localStorage.getItem('user.token');
        const userStore = useUserStore();
        const token = userStore.token;
        if (token == null || token === '') {
            next({ name: 'login' });
        } else {
            next();
        }
    }
})

export default router
